function P=CBW_step_player(G,P,k)
global Board
x=0;y=0;
while 1,
    [x,y,but]=ginput(1);
    x=floor(x+0.5);
    y=floor(y+0.5);
    if but~=1,
        choice=menu('Please choose your action',{'PASS!','Game Over!'});
        if choice==2,
            P.is_gameover=1;
            P.info.String='GAME OVER!';
            drawinfo(P.info);
            return;
        else,
            P.pos=[];
            P.info.String='Pass!';
            drawinfo(P.info);
            return;
        end;
    end;
    [q,cnt]=is_valid(x,y,k);
    if q==1,
        break;
    else,
        menu('Warning: Invalid piece! Please rechoose your piece!',{'OK'});
    end;
end;
P.pos=[x,y];
P.Vars=P.pos;
put_piece(x,y,k,cnt);


function s=scan_all_valid(k)
global Board
N=size(Board,1);
s=0;
for x=1:N,
    for y=1:N,
        if Board(x,y)==0 & is_valid(x,y,k),
            s=s+1;
        end;
    end;
end;


function b=get_b(x,y)
global Board
if 0<x & x<=size(Board,2) & 0<y & y<=size(Board,1),
    b=Board(x,y);
else
    b=-1;
end;

function set_b(x,y,k)
global Board
Board(x,y)=k;
show_piece(x,y,k);


function cnt=chk_direction(x,y,m,k,set_inverse)
global Board
d=[
1,0;
-1,0;
0,1;
0,-1;
1,1;
-1,-1;
1,-1;
-1,1
];
xd=d(m,1);
yd=d(m,2);
xx=x+xd;
yy=y+yd;
cnt=0;
while(get_b(xx,yy)==3-k),
    if set_inverse==1,
        set_b(xx,yy,k);
    end;
    xx=xx+xd;
    yy=yy+yd;
    cnt=cnt+1;
end;
if get_b(xx,yy)~=k,
    cnt=-1;
end;

function [q,cnt]=is_valid(x,y,k)
q=0;cnt=zeros(1,8);
b0=get_b(x,y);
if b0~=0,
    return;
end;
for m=1:8,
    cnt(m)=chk_direction(x,y,m,k,0);
end;
q=~isempty(find(cnt>0));

function put_piece(x,y,k,cnt)
set_b(x,y,k);
for m=1:8,
    if cnt(m)>0,
    chk_direction(x,y,m,k,1);
    end;
end;
